<template>
  <div class="center">
    <div class="box">
      <div class="avatarbox">
        <img :src="bearTitleImg" />
      </div>
      <el-form
        ref="loginform"
        :rules="loginRules"
        :model="loginform"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input v-model="loginform.username" style="width:95%" v-focus></el-input>
        </el-form-item>
        <el-form-item prop="userpassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input style="width:95%" v-model="loginform.userpassword"></el-input>
        </el-form-item>
        <el-form-item style="text-align:center">
          <el-button type="primary" @click="submitForm('loginform')">登录</el-button>
          <el-button type="info" @click="setUserinfo('aa')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import * as config from "../configsetting";
import {post} from '../request';
import { mapActions } from 'vuex';
export default {
  data() {
    return {
      loginform: {
        username: "admin1",
        userpassword: "Pass123"
      },
      loginvalid: false,
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: this.validateUsername }
        ],
        userpassword: [
          {
            required: true,
            trigger: "blur",
            validator: this.validateUserpassword
          }
        ]
      },
      bearTitleImg: config.NodeHost + "/uploads/avatarbear.png"
    };
  },
  mounted() {
    console.log("centervue-mounted");
  },
  methods: {
    validateUsername(rule, value, cb) {
      if (!value || value.length === 0) {
        cb("请检查用户名");
      }
    },
    validateUserpassword(rule, value, cb) {
      let tmp = /(?=.*[A-Za-z])(?=.*\d)[^]{5,8}/.test(value);
      if (!tmp) cb("请检查密码");
    },
    checkBeforSubmit() {
      let t = this.loginform.username.length === 0;
      if(t)
        console.log('用户名为空');
      
      let tmp = /(?=.*[A-Za-z])(?=.*\d)[^]{5,8}/.test(
        this.loginform.userpassword
      );
      if(!tmp)
        console.log('密码不符合规范');
      return t || !tmp;
    },
    submitForm(formname) {
      console.log("onsubmit");
      let form = this.$refs["loginform"];
      if (this.checkBeforSubmit()) {
        this.$message({ message: "请检查用户名密码", type: "warning" });
      } else {
        this.postformserver();
      }
    },
    postformserver() {
      console.log("提交服务器");
      let url = "/trylogin";
      let vm = this;
      post(
        url,
        vm.loginform,
        (res, err) => {
          if (err) {
            console.log("-----------error------------");
            console.log(err);
          } else {
            console.log(res);
            if (res.data.statuscode == 200) {
              vm.setUserinfo(res.data);
              console.log(vm.$store.state.app.token);
              vm.$router.push({ path: "/menuhome/enword" });
            } else {
              vm.$message({ message: res.data.message });
            }
          }
        },
        err => {
          console.log(err);
        }
      );
    },
    ...mapActions('app',['setUserinfo'])
  }
};
</script>
<style scoped>
.center {
  height: 100%;
  background-color: chocolate;
}
.box {
  height: 400px;
  width: 600px;
  background-color: aliceblue;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.avatarbox {
  height: 250px;
  width: 250px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login_form {
  position: absolute;
  bottom: 20px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>